/*
Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package it.kirys.rilego.gui;

import it.kirys.rilego.engine.EngineVersionInfo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.util.Properties;

/**
 * This class ge the version info of the gui tool.
 * @author Federico Improta
 */
public class GuiVersionInfo {
	public static final String APP_NAME = "Rilego";
	public static final String APP_VERSION = getProperty("application.version");
	public static final String APP_FULLNAME = APP_NAME +" "+APP_VERSION;

	/**
	 * This method loads the license file from the package
	 * @return a string containing the license text
	 */
	public static String getLicense(){
		return getTextFileFromJar("license/license.txt");
	}//public static String getLicense()

	/**
	 * This method loads the release history file from the package
	 * @return a string containing the license text
	 */
	public static String getHistory(){
		return getTextFileFromJar("license/history.txt");
	}//public static String getLicense()


	/**
	 * Return the content of a text file got from the JAR package
	 * @param path relative path of the file into the jar
	 * @return a string containing the text
	 */
	public static String getTextFileFromJar(String path){
		try {
			InputStream in = EngineVersionInfo.class.getClassLoader().getResourceAsStream(path);
			StringWriter sw = new StringWriter();
			Reader r = new InputStreamReader(in);
			BufferedReader reader = new BufferedReader(r);
			BufferedWriter writer = new BufferedWriter(sw);
			String line;
			while ((line = reader.readLine()) != null) {
				writer.write(line+"\n");
			}
			writer.flush();
			return sw.toString();
		} catch (IOException ex) {
			return null;
		}
	}//public static String getTextFile(String path)

	/**
	 * Return the value of a property from the rilegoguiappinfo.properties properties file
	 * @param name property name
	 * @return property value if exists null otherwise
	 */
	protected static String getProperty(String name) {
		Properties props = new Properties();
		String data;
		//try retrieve data from file
		try {
			InputStream in = GuiVersionInfo.class.getClassLoader().getResourceAsStream("rilegoguiappinfo.properties");

			props.load(in);

			data = props.getProperty(name);
			return data;
		} //catch exception in case properties file does not exist
		catch (Exception e) {
			return null;
		}
	}//protected static String getProperty(String name)
}
